package com.jzr.board.ui.fragment.main

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayout
import com.jzr.board.R
import com.jzr.board.base.BaseEvenBus
import com.jzr.board.base.BaseEvenBus.GETBOARDGET_T
import com.jzr.board.base.BaseFragment
import com.jzr.board.base.BaseFragmentPageAdapter
import com.jzr.board.bean.BoardGetBean
import com.jzr.board.presenter.contract.TodyWorkFragmentContract
import com.jzr.board.presenter.impl.TodyWorkFragmentImpl
import com.jzr.board.ui.fragment.todyworkfragment.GeneralViewFragment
import com.jzr.board.ui.fragment.todyworkfragment.JobContentFragment
import com.jzr.board.ui.fragment.todyworkfragment.WorkTablefragment
import com.jzr.board.utils.PreferUtil
import kotlinx.android.synthetic.main.fragment_todywork.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.util.*

class TodyWorkFragment : BaseFragment(), TodyWorkFragmentContract.View {

    private val mPresenter = TodyWorkFragmentImpl()

    companion object {
        fun newInstance(): TodyWorkFragment? {
            return TodyWorkFragment()
        }
    }

    private var myAdapter: BaseFragmentPageAdapter? = null
    private val mTitleList = ArrayList<String>()
    private val mFragments: ArrayList<Fragment> = ArrayList<Fragment>()

    override fun getLayoutResId(): Int {
        return R.layout.fragment_todywork
    }

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }

        mTitleList.clear()
        mFragments.clear()

        mPresenter.getBoardGet(
            activity,
            "deptCode",
            PreferUtil.getInstance().deptCode,
            "bedPrefix",
            PreferUtil.getInstance().bedPrefix
        )

    }

    @Subscribe
    fun getEvenBusMessage(baseMessage: BaseEvenBus) {
        when (baseMessage.messageId) {
            BaseEvenBus.GETBOARDGET -> {
                mPresenter.getBoardGet(activity, "deptCode", PreferUtil.getInstance().deptCode)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun getBoardGetSuccess(data: BoardGetBean?) {
        mFragments.clear()
        mTitleList.clear()

        mTitleList.add("一览表")
        mTitleList.add("工作内容")
        mTitleList.add("工作台")

        mFragments.add(GeneralViewFragment.newInstance(data)!!)
        mFragments.add(JobContentFragment.newInstance()!!)
        mFragments.add(WorkTablefragment.newInstance()!!)
        myAdapter = BaseFragmentPageAdapter(childFragmentManager, mFragments, mTitleList)
        vp.adapter = myAdapter
        myAdapter?.notifyDataSetChanged()

        EventBus.getDefault().post(BaseEvenBus(BaseEvenBus.GETBOARDGET_T, data))

        tabLayout.setupWithViewPager(vp)

        tabLayout.tabGravity = TabLayout.GRAVITY_FILL
        tabLayout.tabMode = TabLayout.MODE_SCROLLABLE

    }

    override fun showError(message: String?) {

    }


}